One of the analyses of global and $GLOBALS[] in php

  • 2020-05-10 17:53:23
  • OfStack

This can cause some problems. Some people may inadvertently change a global variable. Global variables in PHP must be declared as global when used in a function (note that the Global keyword is only useful when defined in a function).
1: Global is used to define global variables, but this global variable is not applied to the entire website, but to the current page, including all files of include or require.
 
<?PHP 
$a=123; 
function aa() 
{ 
Global $a; // If you don't $a Is defined as global variable , The inside of a function cannot access the outside of the body $a the , But you can define 1 Same name $a, Now this variable is a local variable, which is the same thing C Language's local variables, which can only be used within the body of a function.  
echo $a; 
} 
aa(); 
?> 

Conclusion: the global variable defined in the body of a function can be used in the body of a function, while the global variable defined in the body of a function cannot be used in the body of a function.
 
$global $a; 
$a=123; 
function f() 
{ 
echo $a; // error , 
} 
// Let's look down here 1 case  
function f() 
{ 
global $a; 
$a=123; 
} 
f(); 
echo $a; // correct , You can use  

2: global
question: I have defined some variables ($a) in config.inc.php. In other files, include(" config.inc.php ") outside the function. Inside the function, I need to use these variables ($a). So you declare global $a, but there are so many functions and so many variables that you can't keep repeating that, right? If you have any good solution, please give me some advice.
answer1: first define the constant in config.inc.php: define(constant name, constant value)
require 'config.inc.php',
Then you can use the constant directly in this file.
answer2: another way I can do this is to define arrays like $x[a], $x, and then just declare global $x1.
answer3: I tried your method, but it didn't work.
answer4: change your php.ini file.
3.1 examples of Global and $GLOBALS arrays

Example: use global
 
<?PHP 
$w3sky = 1; 
$w3sky2 = 2; 
function Sum() 
{ 
global $w3sky, $w3sky2;$w3sky2 = $w3sky + $w3sky2; 
}Sum(); 
echo $w3sky2; 
?> 

The output of the above script will be "3". The global variables $w3sky and $w3sky2 are declared in the function, and all references to any variable will point to the global variable. PHP has no limit on the maximum number of global variables that a function can declare.
The second way to access variables globally is to customize the $GLOBALS array with the special PHP. The previous example can be written as:
The example USES $GLOBALS instead of global
 
<?PHP 
$w3sky = 1; 
$w3sky2 = 2;function Sum() 
{ 
$GLOBALS['w3sky'] = $GLOBALS['w3sky'] + $GLOBALS['w3sky2']; 
}Sum(); 
echo $w3sky2; 
?> 

In the $GLOBALS array, each variable is an element, with the key name corresponding to the variable name and the value corresponding to the contents of the variable. $GLOBALS exists globally because $GLOBALS is a super global variable. The following example shows the use of a super global variable:
Example demonstrates examples of super global variables and scopes
 
<?PHP 
function test_global() 
{ 
//  Most predefined variables do not  "super" They need to be used  'global'  Keywords to make them valid in the local area of the function.  
global $HTTP_POST_VARS;echo $HTTP_POST_VARS['name'];// Superglobals  They are valid on any scale, they are not required  'global'  The statement. Superglobals  Is in the  PHP 4.1.0  The introduction of.  
echo $_POST['name']; 
} 
?> 

global means that if you declare global $db in a file then you can refer to this $db at the bottom of the declaration.
4. It was originally thought that global and $GLOBALS were all alike except for the way they were written. However, in practice, it was found that the difference between the two is still great!
Let's look at the following example:
 
<?php 
//  example 1 
function test_global() { 
global $var1, $var2; 
$var2 =& $var1; 
} 
function test_globals() { 
$GLOBALS['var3'] =& $GLOBALS['var1']; 
} 
$var1 = 5; 
$var2 = $var3 = 0; 
test_global(); 
print $var2 ."\n"; 
test_globals(); 
print $var3 ."\n"; 
?> 

Copy the code
The execution results are as follows:
0
5
How could that be? Shouldn't it be two fives? How do you get a 0 and a 5?
Well, let's take the above questions and dive into the $GLOBALS and global principles!
We all know that variables are actually "symbols" in the corresponding physical memory in the code. Suppose the memory allocated by the three variables we declared above is shown in the following figure:
Explanation of $GLOBALS cited in the php manual:
Global variable: $GLOBALS
Note: $GLOBALS is available in PHP 3.0.0 and later.
An array of all defined global variables. The variable name is the index of the array.
This is an "superglobal" or can be described as an automatic global variable.
That is, the $var1 and $GLOBALS['var1'] in the code above refer to the same variable, not two different variables!
So what does global do?
We all know that the variables generated by functions in php are private variables of functions, so the variables generated by the global keyword cannot escape this rule. Why do you say this?
 
<?php 
//  example 2 
function test() { 
global $a; 
unset($a); 
} 
$a = 1; 
test(); 
print $a; 
?> 

Copy the code
The execution results are as follows:
1
Why would it print 1? I have already paid $a to unset. unset not working? php bug?
No, actually, unset works. It just dropped $a from the test function to unset. You can add it after the function
print $a;
Copy the code
To test! That is to say, global generates the alias variable "$a" for $a outside the test function. To distinguish it from the $a outside, I call it "test-" > $a, so if example 1 is named this way, the following figure can be obtained:
After the execution of test_globals, see the change of the variable:
At this point, if you look at the diagram, you can see why after example 1 is done, $var2 is 0 and $var3 is 5!
Therefore, we draw a conclusion that the difference between global and $GLOBALS[] in the function is as follows:
global generates an alias variable that points to the external variable of the function, not the actual external variable of the function, 1 but changes the address to which the alias variable points, and something unexpected happens (why does it print to 2? This is because the $var1 reference points to the $var2 reference. The value that causes the substance to remain unchanged. In this case, the pointer to $var1 only points to the pointer to $var2, but the pointer has changed by 1, but the value of $var2 has not changed substantially at all, so the value of $var2 still does not change), for example, example 1.
The $GLOBALS[] call is really an external variable, and the inside and outside of the function will always be 1!
Note :(then go back to example 1 above and look at the 1 code in test_global "$var2 =& $var1;") , above is a reference assignment operation, that is, $var2 will point to the physical memory address pointed to by var1. Therefore, after example 1 executes the test_global function, the change of the variable will only have an effect on the local part of the function. Outside the function, the physical memory address pointed to by $var2 does not change.
Going back to example 1 above, look at the 1 code in test_global "$var2 =& $var1;" , above is a reference assignment operation, that is, $var2 will point to the physical memory address pointed to by var1, so after example 1 executes the test_global function, the change of the variable can be seen from the following figure.)
The analysis of this article is not thorough enough. For those who don't understand, please refer to the analysis of global and $GLOBALS[] part 2, in which 1 and 3 are listed for easy understanding

Related articles: